package com.jswlw.service.biz.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jswlw.common.BaseException;
import com.jswlw.common.ResponseStatus;
import com.jswlw.dao.biz.OrderInfoMapper;
import com.jswlw.entity.biz.*;
import com.jswlw.service.biz.*;
import com.jswlw.service.helper.DevApiHelper;
import com.jswlw.service.helper.DistributorHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    OrderInfoMapper mapper;
    @Autowired
    DistributorHelper distributorHelper;
    @Autowired
    DevApiHelper devApiHelper;
    @Autowired
    BillSetService billSetService;
    @Autowired
    DevInfoService devInfoService;
    @Autowired
    DevFilterService devFilterService;
    @Autowired
    FilterInfoService filterInfoService;

    @Override
    public void add(OrderInfo orderInfo) {
        mapper.insertSelective(orderInfo);
    }

    @Override
    public void update(OrderInfo orderInfo) {
        mapper.updateByPrimaryKeySelective(orderInfo);
    }

    @Override
    public void delete(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public OrderInfo find4Id(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OrderInfo> queryList(OrderInfo filter) {
        return mapper.queryList(filter);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void checkOrderAndUpdate(String sn, Integer totalAmount, String tradeNo) {
        OrderInfo orderInfo = mapper.find4Sn(sn);
        if (orderInfo.getPayStatus().equals(1)) {
            return;
        }
        if (Objects.isNull(orderInfo)) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "订单不存在");
        }

        if (!orderInfo.getAmount().equals(totalAmount)) {
            BaseException.sneakyThrows(ResponseStatus.BUSINESS_ERROR.getCode(), "订单金额错误");
        }

        OrderInfo updateFilter = new OrderInfo();
        updateFilter.setId(orderInfo.getId());
        updateFilter.setPayStatus(1);
        updateFilter.setTradeNo(tradeNo);
        mapper.updateByPrimaryKeySelective(updateFilter);

        distributorHelper.deal(orderInfo);

        BillSet billSet = billSetService.find4Id(orderInfo.getBillSetId());
        DevInfo devInfo = devInfoService.find4Id(orderInfo.getDevId());

        if (devInfo.getDevStatus().equals(1)) {
            List<DevFilter> devFilters = devFilterService.find4DevId(devInfo.getId());
            int dyijlxzdz = 0;
            int derjlxzdz = 0;
            int dsanjlxzdz = 0;
            int dsijllzdz = 0;
            int dwujlxzdz = 0;
            for (int i = 0; i < devFilters.size(); i++) {
                DevFilter devFilter = devFilters.get(i);
                FilterInfo filterInfo = filterInfoService.find4Id(devFilter.getFilterId());
                if (devFilter.getSort() == 1) {
                    if (billSet.getBillingModel() == 0) {
                        dyijlxzdz = filterInfo.getFlux();
                    } else if (billSet.getBillingModel() == 1) {
                        dyijlxzdz = filterInfo.getDuration();
                    } else {

                    }
                } else if (devFilter.getSort() == 2) {
                    if (billSet.getBillingModel() == 0) {
                        derjlxzdz = filterInfo.getFlux();
                    } else if (billSet.getBillingModel() == 1) {
                        derjlxzdz = filterInfo.getDuration();
                    } else {

                    }
                } else if (devFilter.getSort() == 3) {
                    if (billSet.getBillingModel() == 0) {
                        dsanjlxzdz = filterInfo.getFlux();
                    } else if (billSet.getBillingModel() == 1) {
                        dsanjlxzdz = filterInfo.getDuration();
                    } else {

                    }
                } else if (devFilter.getSort() == 4) {
                    if (billSet.getBillingModel() == 0) {
                        dsijllzdz = filterInfo.getFlux();
                    } else if (billSet.getBillingModel() == 1) {
                        dsijllzdz = filterInfo.getDuration();
                    } else {

                    }
                } else if (devFilter.getSort() == 5) {
                    if (billSet.getBillingModel() == 0) {
                        dwujlxzdz = filterInfo.getFlux();
                    } else if (billSet.getBillingModel() == 1) {
                        dwujlxzdz = filterInfo.getDuration();
                    } else {

                    }
                }
            }
            //绑定套餐
            devApiHelper.bindPricing(
                    devInfo.getImei(),
                    devInfo.getBillTypeId(),
                    dyijlxzdz,
                    derjlxzdz,
                    dsanjlxzdz,
                    dsijllzdz,
                    dwujlxzdz
            );
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //充值设备
        log.info(JSON.toJSONString(billSet));
        devApiHelper.addMoney(devInfo.getImei(), billSet.getNum());
    }

    @Override
    public Page<OrderInfo> queryPage(OrderInfo orderInfo, Integer offset, Integer limit) {
        PageHelper.startPage(offset, limit);
        return (Page<OrderInfo>) mapper.queryList(orderInfo);
    }
}
